package com.by.yz.activity;

import com.by.yz.Register.DoctorREST;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.R;
import android.content.Context;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.by.yz.chatuidemo.Constant;
import com.by.yz.chatuidemo.ui.ChatActivity;
import com.by.yz.common.MFGT;
import com.by.yz.custom.TechnologyTeamDividerItemDecoration;
import com.by.yz.entity.Account;
import com.by.yz.entity.AskBean;
import com.by.yz.entity.Doctorbean;
import com.by.yz.entity.TechnologyTeam1;
import com.by.yz.entity.UserTest;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.LoginRESTFul;
import com.by.yz.loginUtil.QustionRESFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.ToJSonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by 你只能呵呵 on 2016/10/15.
 */

public class QuestionDetail extends BaseActivity implements View.OnClickListener{

    private  TextView questioner;
    private  TextView question;
    private  Button answer;
    Account account;
    String nickname="";
    AskBean as;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_detail);

        initViews();
        initData();
    }

    private void initViews() {
        setToolbarTitle("问题详情");
        answer=(Button)findViewById(R.id.answer);
        answer.setOnClickListener(this);
        questioner=(TextView) findViewById(R.id.questioner);
        question=(TextView) findViewById(R.id.question);
    }

    private void initData() {

        as=(AskBean) getIntent().getSerializableExtra("question");

        question.setText(as.getContent());

        UserRESTFul userRESTFul=new UserRESTFul();
        userRESTFul.getUser(as.getCreater(), new UserRESTFul.UserPOSTCallBack() {
            @Override
            public void ok(JSONObject response) throws JSONException {
                UserTest userTest;
                JSONObject jsonObject=response;
                userTest=ToJSonObject.jsonToBean(jsonObject.toString(),UserTest.class);
                nickname = userTest.getnickName();
                questioner.setText(nickname);
            }
            @Override
            public void error(String response) {
                Log.e("PCF-user-error","获取用户表失败"+response);
            }
        });

        AccountRESTFul accountRESTFul=new AccountRESTFul();
        accountRESTFul.get(new AccountRESTFul.IhttpCb() {
            @Override
            public void ok(JSONObject response) {
                JSONObject jsonObject=response;
                account=ToJSonObject.jsonToBean(jsonObject.toString(),Account.class);

                UserRESTFul userRESTFul=new UserRESTFul();
                userRESTFul.getUser(account.getLogin(), new UserRESTFul.UserPOSTCallBack() {
                    @Override
                    public void ok(JSONObject response) throws JSONException {
                        UserTest userTest;
                        JSONObject jsonObject=response;
                        userTest=ToJSonObject.jsonToBean(jsonObject.toString(),UserTest.class);
                        if(userTest.getflag() && !account.getLogin().equals(as.getCreater())){
                            answer.setVisibility(View.VISIBLE);
                        }
                    }
                    @Override
                    public void error(String response) {
                        Log.e("PCF-user-error","获取用户表失败"+response);
                    }
                });
            }
            @Override
            public void error(String error) {
                Log.e("PCF-account-error","授权失败"+error);
            }
        });

    }

    public void onClick(View view){
        switch(view.getId()){
            case R.id.answer:
                Intent intent=new Intent(QuestionDetail.this,ChatActivity.class);
                intent.putExtra(Constant.EXTRA_USER_ID,as.getCreater());
                intent.putExtra("userName", nickname);
                startActivity(intent);
                final AskBean as=(AskBean) getIntent().getSerializableExtra("question");
                as.setFlag(true);
                as.setAnswer(account.getLogin());

                JSONObject jsonObject=ToJSonObject.beanToJSONObejct(as);
                QustionRESFul qustionRESFul=new QustionRESFul();
                qustionRESFul.putQuestion(jsonObject, new QustionRESFul.MyPostCallBack() {
                    @Override
                    public void ok(JSONObject response) throws JSONException {
                        Log.e("qwq",response.toString());
                    }
                    @Override
                    public void error(String error) {
                        Log.e("qwq","geng"+error);
                    }
                });
                QuestionDetail.this.finish();
                break;
        }
    }
}
